package si.yun.service.impl;

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import si.yun.dao.PersonalDao;
import si.yun.entity.*;
import si.yun.service.PersonalService;
import si.yun.utils.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;

/**
 * @program: ThinkManage
 * @description: 个人中心业务层
 * @author: 崔晓晨
 * @create: 2021-10-20 14:10
 **/
@Service
@PropertySource({"config/config.properties"})
public class PersonalServiceImpl implements PersonalService {
    @Autowired
    private PersonalDao personalDao;
    //获取服务器路径
    @Value("${path1}")
    private String path;
    @Autowired
    private TestAliUpload testAliUpload;
    //连接redis缓存
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * @Description  查看播放记录
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>> 
     * @Date 2021/10/28 14:47
     **/
    @Override
    public List<Map<String, Object>> selWatchCourse(Integer userId) {
        List<Map<String,Object>> list = new ArrayList<>();
        List<SyPlayRecord> syPlayRecords = personalDao.selWatchCourse(userId);
        for(SyPlayRecord li:syPlayRecords){
            Map<String,Object> map = new HashMap<>();
            map.put("id",li.getId());
            SyCourse sy = personalDao.selCourseInfo(li.getCurriculumId());
            Integer type = sy.getType();
            String true_name = setUserInfo(type, sy.getUserId());
            map.put("trueName",true_name+"老师");
            map.put("courseId",sy.getCourseId());
            map.put("courseName",sy.getCourseName());
            map.put("courseIntro",sy.getCourseIntro());
            if(sy.getCoursePrice().compareTo(BigDecimal.ZERO)==0){
                map.put("coursePrice","免费");
            }else{
                map.put("coursePrice",sy.getCoursePrice());
            }
            if(sy.getCourseNprice().compareTo(BigDecimal.ZERO)==0){
                map.put("courseNprice","免费");
            }else{
                map.put("courseNprice",sy.getCourseNprice());
            }
            map.put("url",path+sy.getUrl());
            list.add(map);
        }
        return list;
    }
    /**
     * @Description  批量删除观看记录
     * @return java.lang.Integer
     * @Date 2021/10/21 11:41
     **/
    @Override
    public Integer delWatchCourse(List<Integer> ids) {
        Integer del = personalDao.delWatchCourse(ids);
        return del;
    }
    /**
     * @Description  获取订单信息
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>> 
     * @Date 2021/11/9 10:03
     **/
    @Override
    public List<Map<String, Object>> selOrderInfo(Integer userId, Integer orderStatus, Integer comment) {
        List<Map<String, Object>> list = personalDao.selOrderInfo(userId, orderStatus, comment);
        for(Map<String, Object> map:list){
            Integer orderType = (Integer) map.get("orderType");
            Integer productId = (Integer) map.get("productId");
            Integer orderId = (Integer) map.get("orderId");
            Integer doUseShoppingCart = (Integer)map.get("doUseShoppingCart");
            if(comment==2){
                //获取当先购买用户是否在指定课程下评论
                Integer commentNum = personalDao.selUserIdCommentNum(userId, productId, orderType);
                if(commentNum>0){
                    Integer upd = personalDao.updCpmmentStatus(3, orderId);
                }else{
                    System.out.println("未评论："+orderId);
                }
            }
            if(orderType==2){//视频
                Map<String, Object> stringObjectMap = personalDao.selCourseOrderInfo(productId);
                map.put("productName",stringObjectMap.get("courseName"));
                map.put("url",path+stringObjectMap.get("url"));
            }else if(orderType==3){//商品
                if(doUseShoppingCart==3){//未使用购物车
                    Map<String, Object> stringObjectMap = personalDao.selProductOrderInfo(productId);
                    map.put("productName",stringObjectMap.get("productName"));
                    map.put("url",path+stringObjectMap.get("url"));
                }else if(doUseShoppingCart==2){//使用了购物车
                    List<Map<String, Object>> selShopOrderShpows = personalDao.selShopOrderShpows(orderId);
                    Integer productNum = 0;
                    for(Map<String, Object> map1:selShopOrderShpows){
                        map1.put("mainImage",path+map1.get("mainImage"));
                        Integer num = (Integer) map1.get("productNum");
                        productNum+=num;
                    }
                    map.put("productCount",productNum);
                    map.put("shopinfo",selShopOrderShpows);
                }
            }
        }
        return list;
    }

    /**
     * @Description  获取商品的订单信息
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @Date 2021/11/11 13:29
     **/
    @Override
    public Map<String, Object> getProductOrderInfo(Integer orderId, Integer productId, Integer orderType) {
        Map<String, Object> map = personalDao.getProductOrderInfo(orderId,productId,orderType);
        map.put("mainImage",path+map.get("mainImage"));
        return map;
    }
    /**
     * @Description  更改订单状态
     * @return java.lang.Integer
     * @Date 2021/11/17 15:08
     **/
    @Override
    public Integer updRefundStatus(Integer orderId,Integer orderStatus) {
        Integer upd = personalDao.updRefundStatus(orderId,orderStatus);
        return upd;
    }
    /**
     * @Description  获取优惠券的列表信息 未使用
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>> 
     * @Date 2021/11/22 15:27
     **/
    @Override
    public List<Map<String, Object>> selCouponListInfo(Integer state, Integer UserId) {
        List<Map<String, Object>> list = personalDao.selCouponListInfo(state, UserId);
        return list;
    }
    /**
     * @Description  获取优惠券的列表信息 已使用
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>> 
     * @Date 2021/11/22 16:23
     **/
    @Override
    public List<Map<String, Object>> selCouponUsed(Integer state, Integer UserId) {
        //获取优惠券的列表信息 已使用
        List<Map<String,Object>> selCouponUsed = personalDao.selCouponUsed(state, UserId);
        return selCouponUsed;
    }
    /**
     * @Description  获取已过期的优惠券信息
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @Date 2021/11/22 16:47
     **/
    @Override
    public List<Map<String, Object>> selExpiredCouponInfo(Integer state, Integer UserId) {
        //获取优惠券的列表信息 已使用
        List<Map<String,Object>> selCouponUsed = personalDao.selExpiredCouponInfo(state, UserId);
        return selCouponUsed;
    }
    /**
     * @Description  添加播放记录
     * @return java.lang.Integer
     * @Date 2021/11/23 11:15
     **/
    @Override
    public Integer addPlayWatch(Integer userId, Integer courseId, String courseName,Integer studyDate) {
        List<SyPlayRecord> syPlayRecords = personalDao.selWatchCourse(userId);
        List<Integer> couId = new ArrayList<>();
        for(SyPlayRecord syPlayRecord:syPlayRecords){
            Integer curriculumId = syPlayRecord.getCurriculumId();
            couId.add(curriculumId);
        }
        if(couId.contains(courseId)){
            Integer upd = personalDao.updPlayWatch(userId, courseId);
            return upd;
        }else{
            Integer upd = personalDao.updStudyMoney(userId, studyDate);
            Integer add = personalDao.addPlayWatch(userId,courseId,courseName);
            return add;
        }
    }
    @Override
    public List<Map<String,Object>> selUserCollection(Integer userId) {
        List<Map<String,Object>> list = new ArrayList<>();
        List<SyUserCollection> syPlayRecords = personalDao.selUserCollection(userId);
        for(SyUserCollection li:syPlayRecords){
            System.out.println( "用户收藏记录" + li);
            Map<String,Object> map = new HashMap<>();
            map.put("userId",li.getUserId());
            map.put("collectionType",li.getCollectionType());
            map.put("collectionDate",li.getCollectionDate());
            map.put("id",li.getCollectionId());
            //课程
            if(li.getCollectionType()==2){
                SyCourse sy = personalDao.selCourseInfo(li.getProductId());
                Integer type = sy.getType();
                String true_name = setUserInfo(type, sy.getUserId());
                map.put("trueName",true_name+"老师");
                map.put("courseId",sy.getCourseId());
                map.put("courseName",sy.getCourseName());
                map.put("courseIntro",sy.getCourseIntro());
                if(sy.getCoursePrice().compareTo(BigDecimal.ZERO)==0){
                    map.put("coursePrice","免费");
                }else{
                    map.put("coursePrice",sy.getCoursePrice());
                }
                if(sy.getCourseNprice().compareTo(BigDecimal.ZERO)==0){
                    map.put("courseNprice","免费");
                }else{
                    map.put("courseNprice",sy.getCourseNprice());
                }
                map.put("url",path+sy.getUrl());
                list.add(map);
            //商品
            }else if(li.getCollectionType()==3){
                SyProduct sy = personalDao.selProductInfo(li.getProductId());
                map.put("recommend",sy.getRecommend());
                map.put("ensure",sy.getEnsure());
                map.put("courseId",sy.getProductId());
                map.put("courseName",sy.getProductName());
                map.put("courseIntro",sy.getProductDescription());
                if(sy.getOriginalpriceProduct().compareTo(BigDecimal.ZERO)==0){
                    map.put("coursePrice","免费");
                }else{
                    map.put("coursePrice",sy.getOriginalpriceProduct());
                }
                if(sy.getCurrentpriceProduct().compareTo(BigDecimal.ZERO)==0){
                    map.put("courseNprice","免费");
                }else{
                    map.put("courseNprice",sy.getCurrentpriceProduct());
                }
                map.put("url",path+sy.getMainImage());
                list.add(map);
            //微圈
            }else if(li.getCollectionType()==4){
                SyForum sy = personalDao.selMicroForumInfo(li.getProductId());
                Integer type = sy.getType();
                if(type==1){
                    SyBeforeUser beforeUser = personalDao.selBeforeUserInfo(userId);
                    String realName = beforeUser.getRealName().substring(0, 1);
                    map.put("trueName",realName+"老师");
                }else if(type==2){
                    //管理员用户
                    SyUser backUserInfo = personalDao.selBackUserInfo(userId);
                    String realName = backUserInfo.getTrueName().substring(0, 1);
                    map.put("trueName",realName+"老师");
                }
                map.put("courseId",sy.getMicrocircleId());
                map.put("courseName",sy.getArticleTitle());
                map.put("courseIntro",sy.getArticleContent());
                map.put("url",path+sy.getArticleUrl());
                list.add(map);
            }
        }
        return list;
    }
    /**
     * @Description  绑定人员数据
     * @return java.lang.String 
     * @Date 2021/11/24 14:11
     **/
    public String setUserInfo(Integer type,Integer userId){
        if(type==2){
            SyBeforeUser beforeUser = personalDao.selBeforeUserInfo(userId);
            String realName = beforeUser.getRealName().substring(0, 1);
            return realName;
        }else if(type==3){
            //管理员用户
            SyUser backUserInfo = personalDao.selBackUserInfo(userId);
            String realName = backUserInfo.getTrueName().substring(0, 1);
            return realName;
        }else{
            //管理员用户
            SyUser user = personalDao.selBackUserInfo(userId);
            String realName = user.getTrueName().substring(0, 1);
            return realName;
        }
    }
    /**
     * @Description  获取用户购买的课程信息
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @Date 2021/11/25 8:58
     **/
    @Override
    public List<Map<String, Object>> selBuyCourse(Integer userId) {
        List<Map<String, Object>> list = personalDao.selBuyCourse(userId);
        for(Map<String, Object> li:list){
            li.put("url",path+li.get("url"));
        }
        return list;
    }
    /**
     * @Description  获取当前用户是否认证过创作者
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @Date 2021/11/25 9:31
     **/
    @Override
    public Map<String, Object> selApplyRecord(Integer userId) {
        Map<String,Object> map = personalDao.selApplyRecord(userId);
        return map;
    }
    /**
     * @Description  获取课程的分类
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>> 
     * @Date 2021/11/25 15:38
     **/
    @Override
    public List<Map<String, Object>> selCourseClass() {
        List<Map<String, Object>> list = personalDao.selCourseClass();
        return list;
    }
    /**
     * @Description  添加课程 (或者保存为草稿状态)
     * @return java.lang.Integer
     * @Date 2021/11/25 16:50
     **/
    @Override
    public Integer addCourseInfo(SyCourse course) {
        //添加课程 (或者保存为草稿状态)
        Integer add = personalDao.addCourseInfo(course);
        return add;
    }
    /**
     * @Description  获取个人创作的课程
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @Date 2021/11/26 10:12
     **/
    @Override
    public List<Map<String, Object>> selCreationCourse(Integer userId) {
        List<Map<String, Object>> list = personalDao.selCreationCourse(userId);
        for(Map<String, Object> li:list){
            li.put("courseCover",li.get("url"));
            li.put("courseVideo",li.get("courseUrl"));
            String url = (String) li.get("url");
            if(url.length () == 0){
                li.put("url",false);
            }else{
                li.put("url",path+li.get("url"));
            }
            String courseUrl = (String) li.get("courseUrl");
            if(courseUrl.length () == 0){
                li.put("courseUrl",false);
            }else{
                li.put("courseUrl",path+li.get("courseUrl"));
            }
            String courseName = (String) li.get("courseName");
            if(courseName.length () == 0){
                li.put("courseName",false);
            }
        }
        return list;
    }
    /**
     * @Description  修改课程信息
     * @return java.lang.Integer
     * @Date 2021/11/26 14:06
     **/
    @Override
    public Integer updCourseInfo(@RequestParam Map<String,Object> map) {
        return personalDao.updCourseInfo(map);
    }
    /**
     * @Description  获取认证信息
     * @return java.util.Map<java.lang.String,java.lang.Object> 
     * @Date 2021/11/29 18:50
     **/
    @Override
    public Map<String, Object> selReviewInfo(Integer userId) {
        return personalDao.selReviewInfo(userId);
    }

    /**
     * 获取用户的默认地址信息
     * @param userId 用户id
     * @return
     */
    @Override
    public Map<String, Object> getUserAddressIsDefault(Integer userId) {
        Map<String, Object> map = personalDao.getUserAddressIsDefault(userId);
        return map;
    }

    /**
     * 修改用户的手机号，需判断是否在当前日期的三十天之前
     * @param userId
     * @param phone
     * @return
     */
    @Override
    public Integer setUserPhone(Integer userId, String phone) {
        Date date = new Date();
        String lastDate = "";
        String newDate = "";
        try {
            //当前日期
            newDate = Utils.getDayString(Utils.getDateToTimeString(date));
            //处理日期
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            //计算前一个月的日期
            calendar.add(Calendar.MONTH, -1);
            date = calendar.getTime();
            //前一个月的日期
            lastDate = Utils.getDayString(Utils.getDateToTimeString(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return personalDao.setUserPhone(userId, phone, newDate,lastDate);
    }
    /**
     * 修改用户的密码
     * @param userId
     * @param password
     * @return
     */
    @Override
    public Integer setUserPassword(Integer userId, String password) {
        //加密密码
        password = Md5Utils.MD5andSAL(password);
        return personalDao.setUserPassword(userId, password);
    }
    /**
     * 修改用户的实名认证
     * @param userId
     * @param realName
     * @param idCard
     * @return
     */
    @Override
    public SyBeforeUser setUserReal(Integer userId, String realName, String idCard) {
        Integer i = personalDao.setUserReal(userId, realName, idCard);
        SyBeforeUser user = personalDao.getUserInfo(userId);
        if(i > 0){
            return user;
        }
        return null;
    }
    /**
     * 查看用户的所有收货地址
     * @param userId
     * @return
     */
    @Override
    public List<Map<String,Object>> getUserAddress(Integer userId) {
        return personalDao.getUserAddress(userId);
    }
    /**
     * 修改用户的地址是否为默认状态，2为默认，3为不默认，默认是唯一所以要删除用户其他的默认地址
     * @param userId
     * @param addressId
     * @param isDefault
     * @return
     */
    @Override
    public Integer setDefaultAddress(Integer userId, Integer addressId, String isDefault) {
        if("2".equals(isDefault)){
            Integer i ;
            i = personalDao.setDefaultAddress(userId, addressId, isDefault);
            i += personalDao.removeDefaultAddress(userId, addressId, "3");
            return i;
        }else if("3".equals(isDefault)){
            return personalDao.setDefaultAddress(userId, addressId, isDefault);
        }
        return null;
    }
    /**
     * 获取省市区
     * @param id
     * @return
     */
    @Override
    public List<Map<String, Object>> getRegion(Integer id) {
        List<Map<String, Object>> province = personalDao.getRegion(id);
        return province;
    }
    /**
     * 添加用户地址
     * @param userAddress
     * @return
     */
    @Override
    public Integer addUserAddress(SyUserAddress userAddress) {
        return personalDao.addUserAddress(userAddress);
    }
    /**
     * 通过id查看收货地址
     * @param addressId
     * @return
     */
    @Override
    public Map<String, Object> getUserAddres(Integer addressId) {
        return personalDao.getUserAddres(addressId);
    }
    /**
     * 修改用户地址
     * @param userAddress
     * @return
     */
    @Override
    public Integer setUserAddress(SyUserAddress userAddress) {
        return personalDao.setUserAddress(userAddress);
    }
    /**
     * 通过id删除用户地址
     * @param addressId
     * @return
     */
    @Override
    public Integer removeUserAddress(Integer addressId) {
        return personalDao.removeUserAddress(addressId);
    }
    @Override
    public Integer addUserFeedback(SyUserFeedback userFeedback) {
        return personalDao.addUserFeedback(userFeedback);
    }
    /**
     * @return java.util.List<java.util.Map   <   java.lang.String   ,   java.lang.Object>>
     * @Description 获取个人微圈列表
     * @Date 2021/11/26 10:12
     **/
    @Override
    public List<Map<String, Object>> selMicroCircleOwnData(Integer userId) {
        //获取个人微圈列表
        List<Map<String, Object>> m = personalDao.selMicroCircleOwnData(userId);
        return m;
    }
    /**
     * @Description  添加审核创作者
     * @return java.lang.Integer
     * @Date 2021/12/2 8:51
     **/
    @Override
    public Integer addCreateUser(Map<String, Object> map) {
        return personalDao.addCreateUser(map);
    }
    /**
     * @Description  获取系统最新通知信息
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @Date 2021/12/2 10:40
     **/
    @Override
    public Map<String, Object> selNoteNewInfo(Integer userId) {
        return personalDao.selNoteNewInfo(userId);
    }
    /**
     * @Description  获取所有的系统通知
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @Date 2021/12/2 12:00
     **/
    @Override
    public List<Map<String, Object>> selAllNotesInfo(Integer userId) {
        return personalDao.selAllNotesInfo(userId);
    }
    /**
     * @Description  获取未读消息数量
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @Date 2021/12/2 16:11
     **/
    @Override
    public Map<String, Object> selNoteNum(Integer userId) {
        return personalDao.selNoteNum(userId);
    }
    /**
     * @Description  批量更新消息状态信息
     * @return java.lang.Integer 
     * @Date 2021/12/2 16:52
     **/
    @Override
    public Integer updNoteStatus(Integer id) {
        return personalDao.updNoteStatus(id);
    }
    /**
     * @Description  获取用户学习时长以及消费记录
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @Date 2021/12/14 9:38
     **/
    @Override
    public Map<String, Object> selStudyMoney(Integer userId) {
        return personalDao.selStudyMoney(userId);
    }
    /**
     * 通过id查看用户信息
     * @param userId
     * @return
     */
    @Override
    public SyBeforeUser getUserInfo(Integer userId) {
        return personalDao.getUserInfo(userId);
    }

    /**
     * 修改初次注册的用户的手机号
     * @param userId
     * @param phone
     * @return
     */
    @Override
    public Integer setRegisterUserPhone(Integer userId, String phone) {
        String date = null;
        try {
            date = Utils.getDateToTimeString(new Date());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return personalDao.setRegisterUserPhone(userId, phone,date);
    }
    /**
     * @Description  获取当前订单是否使用了优惠券
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @Date 2021/12/15 19:59
     **/
    @Override
    public Map<String, Object> selOrderCouponNum(Integer orderId, Integer userId) {
        Map<String, Object> map = personalDao.selOrderCouponNum(orderId, userId);
        return map;
    }
    /**
     * @Description  获取优惠券的详细信息
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @Date 2021/12/15 19:36
     **/
    @Override
    public Map<String, Object> selCouponInfo(Integer orderId,Integer userId) {
        return personalDao.selCouponInfo(orderId,userId);
    }
    /**
     * 判断手机号是否已被注册
     * @param phone
     * @return
     */
    @Override
    public Integer getPhoneExist(String phone) {
        return personalDao.getPhoneExist(phone);
    }
    /**
     * 通过用户上传身份证证件照片获取信息修改用户的实名认证
     * @param userId
     * @param frontPath
     * @param backPath
     * @return
     */
    @Override
    public SyBeforeUser setUserRealByImg(Integer userId,String frontPath,String backPath) {
        //将图片URL传入工具类处理
        Map<String, Object> map = ApiOrcUtil.getPictureString(frontPath, backPath);
        if (map != null){
            //获取通过图片获取的身份证信息，name为姓名，idCard为身份证号码
            String realName = (String) map.get("name");
            String idCard = (String) map.get("idCard");
            //将身份正反面路径存储
            frontPath = frontPath.replace(path,"");
            backPath = backPath.replace(path,"");
            //判断身份证号是否已被注册
            Integer idCardExist = personalDao.getIdCardExist(idCard);
            //若为零没有被注册过
            if(idCardExist == 0){
                //添加到数据库中，i不为零修改成功
                Integer i = personalDao.setUserRealByImg(userId, realName, idCard,frontPath,backPath);
                //重新查询数据
                SyBeforeUser user = personalDao.getUserInfo(userId);
                if(i > 0){
                    return user;
                }
            }else {
                return null;
            }
        }
        return null;
    }
    /**
     * @Description  更新最新的APP版本
     * @return java.util.Map<java.lang.String,java.lang.Object> 
     * @Date 2021/12/27 13:28
     **/
    @Override
    public Map<String, Object> selAppVersion() {
        Map<String, Object> map = personalDao.selAppVersion();
        map.put("filePath",path+map.get("filePath"));
        return map;
    }
    /**
     * @Description  删除课程草稿
     * @return java.lang.Integer
     * @Date 2021/12/28 9:08
     **/
    @Override
    public Integer delCourseNutton(Integer courseId) {
        //删除草稿前判断是否有参与的课程封面 和 视频 并在服务器中删除
        Map<String,Object> map = personalDao.selCourseVideo(courseId);
        String url = (String) map.get("url");
        String courseUrl = (String) map.get("courseUrl");
        if(url.length()!=0){
            testAliUpload.delete(url);
        }
        if(courseUrl.length()!=0){
            testAliUpload.delete(courseUrl);
        }
        Integer del = personalDao.delCourseNutton(courseId);
        return del;
    }
    /**
     * @Description  查询商品的详情信息
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @Date 2022/1/4 10:30
     **/
    @Override
    public Map<String, Object> selProductInfoes(Integer productId) {
        Map<String, Object> map = personalDao.selProductInfoes(productId);
        String mainImage = (String) map.get("mainImage");
        if(mainImage!=null && mainImage!=""){
            map.put("mainImage",path+mainImage);
        }
        Map newImg = new HashMap();
        newImg.put("pictureUrl",mainImage);
        //查询商品下的附属图片
        List<Map<String, Object>> list = personalDao.selProcudtMirrorImg(productId);
        list.add(newImg);
        if (list!=null){
            for(Map<String, Object> mao:list){
                String pictureUrl = (String) mao.get("pictureUrl");
                if(pictureUrl!=null && pictureUrl!=""){
                    mao.put("pictureUrl",path+pictureUrl);
                }
            }
        }
        map.put("pictureUrl",list);
        return map;
    }
    /**
     * @Description  查询指定分类下的商品
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @Date 2022/1/5 13:54
     **/
    @Override
    public List<Map<String, Object>> selClassProductInfoes(Integer categoryId,Integer productId) {
        List<Map<String, Object>> list = personalDao.selClassProductInfoes(categoryId);
        for(int a=0;a<list.size();a++){
            Integer pId = (Integer) list.get(a).get("productId");
            if(productId==pId){
                list.remove(a);
            }
        }
        for(Map<String, Object> map : list){
            String mainImage = (String) map.get("mainImage");
            if(mainImage.length()!=0 && mainImage!=null &&mainImage!=""){
                map.put("mainImage",path+mainImage);
            }
        }
        return list;
    }
    /**
     * @Description  获取评论的数量
     * @return java.util.Map<java.lang.String,java.lang.Integer>
     * @Date 2022/1/6 11:26
     **/
    @Override
    public Map<String, Integer> selCommentProductNum(Integer productId) {
        return personalDao.selCommentProductNum(productId);
    }
    /**
     * @Description  获取商品下的评论
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @Date 2022/1/6 16:55
     **/
    @Override
    public List<Map<String, Object>> selproductComments(Integer productId,Integer userId) {
        List<Map<String, Object>> productComments = personalDao.selproductComments(productId);
        List<Map<String, Object>> list1 = personalDao.selCommentLikeIf(productId, userId);
        //处理铭感词
        KeyWordFilter.initPattern();
        for(Map<String,Object> list:productComments){
            Integer userType = (Integer) list.get("userType");
            Integer userIdes = (Integer) list.get("userId");
            if(userType==2){
                SyBeforeUser beforeUser = personalDao.selBeforeUserName(userIdes);
                list.put("nickname",beforeUser.getNickname());
                if(beforeUser.getAvatarUrl()!=null && beforeUser.getAvatarUrl().length()!=0 && beforeUser.getAvatarUrl()!="null"){
                    if(beforeUser.getAvatarUrl().indexOf("http") == -1){
                        list.put("avatarUrl",path+beforeUser.getAvatarUrl());
                    }else{
                        list.put("avatarUrl",beforeUser.getAvatarUrl());
                    }
                }else{
                    list.put("avatarUrl","");
                }
            }else if(userType==3){
                SyUser user = personalDao.selBackUserName(userIdes);
                list.put("nickname",user.getTrueName());
                if(user.getAvatarUrl()!=null && user.getAvatarUrl().length()!=0){
                    if(user.getAvatarUrl().indexOf("http") == -1){
                        list.put("avatarUrl",path+user.getAvatarUrl());
                    }else{
                        list.put("avatarUrl",user.getAvatarUrl());
                    }
                }else{
                    list.put("avatarUrl","");
                }
            }
            String content = (String) list.get("content");
            String newContent = KeyWordFilter.doFilter2(content);
            list.put("content",newContent);
            list.put("isLikeShow",false);
        }
        for(int a=0;a<productComments.size();a++){
            int id = (int) productComments.get(a).get("id");
            for(int i = 0;i<list1.size();i++){
                int commentLikeId = (int) list1.get(i).get("commentId");
                if(commentLikeId==id){
                    productComments.get(a).put("isLikeShow",true);
                }
            }
        }
        return productComments;
    }
    /**
     * @Description  点赞评论
     * @return java.lang.Integer
     * @Date 2022/1/6 19:54
     **/
    @Override
    public Integer addProductCommentLike(Integer commentId, Integer userId, Integer productId) {
        Integer add = personalDao.addProductCommentLike(commentId, userId, productId);
        if(add!=0){
            personalDao.updProductCommentCountNum(productId, commentId);
        }
        return add;
    }
    /**
     * @Description  取消点赞评论
     * @return java.lang.Integer
     * @Date 2022/1/6 19:54
     **/
    @Override
    public Integer delProductCommentLike(Integer commentId, Integer userId, Integer productId) {
        Integer del = personalDao.delProductCommentLike(commentId, userId, productId);
        if(del!=0){
            personalDao.updProductCommentCountNum(productId, commentId);
        }
        return del;
    }
    /**
     * @Description  添加留言
     * @return java.lang.Integer
     * @Date 2022/1/7 9:07
     **/
    @Override
    public Integer addProductCommentInfo(Integer userId, Integer productId, String content) {
        Integer add = personalDao.addProductCommentInfo(userId, productId, content);
        return add;
    }
    /**
     * @Description   删除个人评论 更改状态为删除
     * @return java.lang.Integer
     * @Date 2022/1/7 9:07
     **/
    @Override
    public Integer updProductCommentStatusDel(Integer commentId, Integer userId, Integer productId) {
        //删除个人评论 更改状态为删除
        Integer upd = personalDao.updProductCommentStatusDel(commentId,userId,productId);
        delProductCommentLikeStatus(commentId, productId);
        return upd;
    }
    /**
     * @Description  删除被删除的评论的点赞量
     * @return java.lang.Integer
     * @Date 2022/1/7 9:07
     **/
    @Transactional
    public Integer delProductCommentLikeStatus(Integer commentId, Integer courseId){
        Integer del = personalDao.delProductCommentLikeStatus(commentId, courseId);
        return del;
    }
    /**
     * @Description  判断商品是否收藏
     * @return java.lang.Integer
     * @Date 2022/1/7 9:56
     **/
    @Override
    public Integer selProductColCourseIF(Integer userId, Integer productId) {
        return personalDao.selProductColCourseIF(userId,productId);
    }
    /**
     * @Description  收藏商品
     * @return java.lang.Integer
     * @Date 2022/1/7 10:31
     **/
    @Override
    public Integer addProductColCourseInfo(Integer userId, Integer productId) {
        //添加记录
        Integer add = personalDao.addProductColCourseInfo(userId, productId);
        updProductColNum(productId);
        return add;
    }
    /**
     * @Description  取消收藏
     * @return java.lang.Integer
     * @Date 2022/1/7 10:32
     **/
    @Override
    public Integer delProductColCourseInfo(Integer userId, Integer productId) {
        Integer del = personalDao.delProductColCourseInfo(userId, productId);
        updProductColNum(productId);
        return del;
    }
    /**
     * @Description  同步数据
     * @return java.lang.Integer
     * @Date 2022/1/7 10:30
     **/
    @Transactional
    public Integer updProductColNum(Integer productId){
        Integer integer = personalDao.updProductColNum(productId);
        return integer;
    }
    /**
     * @Description  获取购物车的商品数量
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @Date 2022/1/7 15:36
     **/
    @Override
    public Map<String, Object> selShoppingCart(Integer userId) {
        return personalDao.selShoppingCart(userId);
    }
    /**
     * @Description  获取购物车的详细信息
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @Date 2022/1/10 9:41
     **/
    @Override
    public List<Map<String, Object>> selShopChartInfo(Integer userId) {
        List<Map<String, Object>> list = personalDao.selShopChartInfo(userId);
        if(list.size()!=0){
            for(Map<String,Object> map:list){
                String mainImage = (String) map.get("mainImage");
                if(mainImage.length()!=0 && mainImage!=null && mainImage!=""){
                    map.put("mainImage",path+mainImage);
                }
            }
        }
        return list;
    }
    /**
     * @Description  更新购物车的商品数量
     * @return java.lang.Integer
     * @Date 2022/1/10 11:27
     **/
    @Override
    public Integer updShopChartNum(Integer shoppingCartId, Integer productNum) {
        return personalDao.updShopChartNum(shoppingCartId, productNum);
    }
    /**
     * @Description  批量删除购物车
     * @return java.lang.Integer 
     * @Date 2022/1/10 16:45
     **/
    @Override
    public Integer deleteShoppingCart(List<Integer> list) {
        Integer del = personalDao.deleteShoppingCart(list);
        return del;
    }
    /**
     * @Description  添加购物车
     * @return java.lang.Integer
     * @Date 2022/1/10 18:40
     **/
    @Override
    public Integer addShoppingCart(Integer userId, Integer productId) {
        return personalDao.addShoppingCart(userId,productId);
    }
    /**
     * @Description  判断当前用户是否添加指定商品到购物车
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @Date 2022/1/10 19:00
     **/
    @Override
    public Integer selShopProduct(Integer userId, Integer productId) {
        return personalDao.selShopProduct(userId,productId);
    }
    /**
     * @Description  获取微商城列表
     * @return java.lang.Integer
     * @Date 2021/12/28 9:08
     **/
    @Override
    public List<Map<String, Object>> selMicroMall() {
        List<Map<String, Object>> selMicroMall=personalDao.selMicroMall();
        //redis缓存
        ValueOperations<String,String> vo = redisTemplate.opsForValue();
        Gson gson = new Gson();
        vo.set("selMicroMall", gson.toJson(selMicroMall));
        return selMicroMall;
    }
    /**
     * @Description  获取购物车下对应的商品
     * @return java.lang.Integer
     * @Date 2022/1/14 16:46
     **/
    @Override
    public Integer selshopProductId(Integer shoppingCartId) {
        return personalDao.selshopProductId(shoppingCartId);
    }

    /**
     * @Description  获取商品订单所需要的信息
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @Date 2022/1/14 16:27
     **/
    @Override
    public Map<String, Object> selOrderProInfo(Integer productId,Integer shoppingCartId) {
        return personalDao.selOrderProInfo(productId,shoppingCartId);
    }
}
